package sheshen.come.cn.sheshen;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.GenericsCallback;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import okhttp3.Call;
import sheshen.come.cn.sheshen.base.BaseActivity;
import sheshen.come.cn.sheshen.base.url.Url;
import sheshen.come.cn.sheshen.been.HomeBotomListBean;
import sheshen.come.cn.sheshen.been.User;
import sheshen.come.cn.sheshen.inner.http.JsonGenericsSerializator;
import sheshen.come.cn.sheshen.utils.LogUtil;
import sheshen.come.cn.sheshen.utils.Tools;

/**
 * Created by MyPC on 2017/2/17.
 */

public class LoginActivity  extends BaseActivity implements View.OnClickListener{

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    //账号;
    private EditText et_sign_in_account;
    //密码
    private EditText et_sign_in_pwd;
    //用户名;
    private String useName = null;
    //密码
    private String passworld = null;
    //登录
    private Button btn_sign_in;
    private TextView logn_jihuo_tv;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        logn_jihuo_tv = (TextView) findViewById(R.id.logn_jihuo_tv);
        et_sign_in_account = (EditText) findViewById(R.id.et_sign_in_account);
        et_sign_in_pwd = (EditText) findViewById(R.id.et_sign_in_pwd);
        btn_sign_in= (Button) findViewById(R.id.btn_sign_in);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListeners() {
        logn_jihuo_tv.setOnClickListener(this);
        btn_sign_in.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_sign_in:
                //用户名
                useName = et_sign_in_account.getText().toString();
                //密码
                passworld = et_sign_in_pwd.getText().toString();
//                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//                startActivity(intent);

//                OkHttpUtils.get()
//                        .url("http://s.66wedding.com/WeddingMobileApi/get_ProductListIndex?Token=00&pageIndex=1&memberID=0")
//                        .build()
//                        .execute(new GenericsCallback<HomeBotomListBean>(new JsonGenericsSerializator()) {
//                            @Override
//                            public void onError(Call call, Exception e, int id) {
//
//                            }
//
//                            @Override
//                            public void onResponse(HomeBotomListBean response, int id) {
//                                LogUtil.e("===LoginActivity===",response.toString());
////                                Document doc = Jsoup.parse(response);
////                                Elements items = doc.getElementsByClass("errorMessage"); //class = item
////                                String s =  items.text();
////                                if (!TextUtils.isEmpty(s)){
////                                    Toast.makeText(LoginActivity.this,s,Toast.LENGTH_LONG).show();
////                                }else{
////                                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
////                                    startActivity(intent);
////                                }
//
//                            }
//                        });
                MyApplication.getContext().setId(useName);
                OkHttpUtils.post().url("http://192.168.0.166/TestLog/UserServlet.do?")
                        .addParams("userName",useName)
                        .addParams("passWord",passworld)
                        .build()
                        .execute(new GenericsCallback<User>(new JsonGenericsSerializator()) {

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(User response, int id) {
                        Log.e("====你好啦========",response.getDate().toString());
                        String res = response.getRes();
                        Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_LONG).show();
                        switch (res){
                            case "0000":
                                MyApplication.getContext().setUser(response);
                                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                startActivity(intent);
                                break;
                        }

                    }
                });

  //              http://192.168.0.162/TestLog/UserServlet.do?userName=zhangsan&passWord=123

//                OkHttpUtils.post().url("http://192.168.0.166/TestLog/UserServlet.do?")
//                        .addParams("userName",useName)
//                        .addParams("passWord",passworld)
//                        .build()
//                        .execute(new MyStringCallback());
//
//                OkHttpUtils.post()
//                        .url("http://192.168.0.166/TestLog/FindAll.do")
//                        .addParams("NewLog","dnogndjgodjgojdogjd")
//                        .build().execute(new MyStringCallback());

                break;
            case R.id.logn_jihuo_tv:
                View view = View.inflate(this,R.layout.view_dialog_set,null);
                Button btn_cofig = (Button) view.findViewById(R.id.btn_config);
                btn_cofig.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                Button btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
                btn_cancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Tools.dismissWaitDialog();
                    }
                });
                Tools.showCustomDialog(view,this);
                break;
        }
    }
    public class MyStringCallback extends StringCallback{

        @Override
        public void onError(Call call, Exception e, int id) {

        }

        @Override
        public void onResponse(String response, int id) {
            Log.e("---------------",response);
        }
    }
}
